﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Test_Drag_N_drop
{
    public class ConstructBlock
    {
        BasicEffect basicEffect;
        VertexPositionColor[] vertices;

        protected TypeOfBlock type;

        public TypeOfBlock BlockType
        {
            get { return type; }
            set {type = value;}
        }

        protected int myID;
        public int ID
        {       
           get { return myID; }
           set { myID = value; }
        }

        protected BlockDnD son;

        public BlockDnD Son
        {
            get { return son; }
            set { son = value; }
        }
        protected bool haveSon = false;

        public bool HaveSon
        {
            get { return haveSon; }
            set { haveSon = value; }
        }

        protected Texture2D _texture;
        protected Texture2D text;
        protected Rectangle rectangle;

        public Rectangle Rect
        {
            get { return rectangle; }
            set { rectangle = value; }
        }


        protected Texture2D textureIdle;
        protected Texture2D textureOver;



        public Texture2D Texture
        {
            get { return _texture; }
            set { _texture = value; }
        }
        
        public ConstructBlock (int id)
        {
            myID = id;
        }

        public virtual void Initialize(GraphicsDevice graphic, int x, int y, TypeOfBlock type)
        {
            rectangle = new Rectangle(x, y, 0, 0);
            this.type = type;
            
            basicEffect = new BasicEffect(graphic);
            basicEffect.VertexColorEnabled = true;
            basicEffect.Projection = Matrix.CreateOrthographicOffCenter
               (0, graphic.Viewport.Width,     // left, right
                graphic.Viewport.Height, 0,    // bottom, top
                0, 1);                                         // near, far plane
            vertices = new VertexPositionColor[2];
        }


        public virtual void LoadContent(ContentManager content)
        {
            textureIdle = content.Load<Texture2D>("Idle");
            textureOver = content.Load<Texture2D>("Over");
            _texture = textureIdle;
            rectangle.X = rectangle.X - _texture.Width / 2;
            rectangle.Y = rectangle.Y - _texture.Height / 2;
            rectangle.Width = _texture.Width;
            rectangle.Height = _texture.Height;

        }


        public virtual void Update(GameTime gameTime, MouseState ms, MouseState pms)
        {
 
        }


    
        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime, GraphicsDevice graphic)
        {
            if (haveSon)
            {
                vertices[0].Position = new Vector3(rectangle.X + rectangle.Width/2, rectangle.Y +rectangle.Height, 0);
                vertices[0].Color = Color.Black;
                vertices[1].Position = new Vector3(son.rectangle.X +rectangle.Width /2, son.rectangle.Y, 0);
                vertices[1].Color = Color.Black;

                basicEffect.CurrentTechnique.Passes[0].Apply();
                graphic.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vertices, 0, 1);

            }

            spriteBatch.Draw(_texture, rectangle, Color.White);
            spriteBatch.Draw(text, rectangle, Color.White);



           
        }
    
        public void isFatherOf(BlockDnD son)
        {
            this.son = son;
            haveSon = true;
        }

        public void isCurrentBlock(bool b)
        {
            if (b)
            {
                _texture = textureOver;
            }
            else
            {
                _texture = textureIdle;
            }
        }
    }
}
